//
//  LoginViewController.m
//  uneed
//
//  Created by apple on 4/9/12.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "LoginViewController.h"
#import "howcanApi2Get.h"
#import "JSONKit.h"

#define API_DoLogin @"100"

@implementation LoginViewController

@synthesize username,password;
@synthesize api2GetLogin;


- (void)dealloc {
    [api2GetLogin release];
	[username release];
	[password release];
    [super dealloc];
}

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	self.title = @"用户登录";
	[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"cookies"];
	[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"is_login"];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


-(IBAction)doLogining:(id)sender {
    if (self.api2GetLogin==nil) {
        howcanApi2Get *tmp_api = [[howcanApi2Get alloc] init];
        self.api2GetLogin = tmp_api;
        [tmp_api release];
        
    }[api2GetLogin loging:username.text withPassword:password.text withDelegate:self withIndex:API_DoLogin];
	
}

-(IBAction)getPasswd:(id)sender {
	
}


-(IBAction)cancelLogin:(id)sender {
	[self dismissModalViewControllerAnimated:YES];
}

/*数据下载完毕 */
-(void)  completedCallback:(NSArray *) data {
	if ([data count]<2) return;
	NSString *theIndex = [data objectAtIndex:0];
    if ([theIndex isEqualToString:API_DoLogin]) {
	NSString *_data = [NSString stringWithFormat:@"%@",[data objectAtIndex:1]];
    NSMutableDictionary *jsondic = [_data objectFromJSONString];
	NSNumber *errCode = [jsondic objectForKey:@"errCode"];
	if (errCode!=NULL && [errCode intValue]==0) {
		[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"is_login"];
//		[self.navigationController popViewControllerAnimated:YES];
		[self dismissModalViewControllerAnimated:YES];
	} else {
		[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"is_login"];
		[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"cookies"];
	}
    }
    
}


@end
